#coding=utf-8
#這是lonote的treeView

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class EditorDelegate(QItemDelegate):
    '''用這個來限制只有哪些格子可以更名！帥！'''
    def __init__(self, parent = None):
        super().__init__(parent)
    def createEditor(self, parent, option, index):
        if index.column() == 0:
            return super().createEditor(parent, option, index)

class LoTreeView(QTreeWidget):
    ''''''
    SIGNALcommitData = pyqtSignal(tuple)
    SIGNALdropData = pyqtSignal(QTreeWidgetItem)
    def __init__(self, parent = None):
        ''''''
        super().__init__(parent)
        self.header().sectionClicked.connect(self.sortByColumn)
        self.header().setClickable(True)
        self.setDropIndicatorShown(True)
        self.modcount = 0
        self.crecount = 0
        self.charcount = 0
        #self.setSelectionMode(QAbstractItemView.SingleSelection)
        #self.viewport().setAcceptDrops(True)

    def commitData(self, editor):
        id = self.currentItem().text(1)
        #執行更名
        self.SIGNALcommitData.emit((editor.text(),id))

    def mouseReleaseEvent(self, event):
        '''當滑鼠放開時（click信號送出），決定點擊的是哪顆按鈕'''
        index = self.indexAt(event.pos())
        #中鍵執行更名任務
        if index.column() == 0:
            if event.button() == Qt.MidButton:
                self.setCurrentIndex(index)
                self.edit(index)

        return super().mouseReleaseEvent(event)

    def sortByColumn(self, column):
        if column == 3:
            self.modcount += 1
            return super().sortItems(5, self.modcount%2)
        elif column == 4:
            self.crecount += 1
            return super().sortItems(6, self.crecount%2)
        elif column == 7:
            self.charcount += 1
            return super().sortItems(8, self.charcount%2)
        return super().sortByColumn(column)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.startPoint = event.pos()
        super().mousePressEvent(event)

    def dragEnterEvent(self, e):
        if (self.currentItem().parent() == 0):
            e.ignore()
        else:
            QTreeWidget.dragMoveEvent(self, e)

    def dropMimeData(self, parent, index, data, action):
        Item = self.currentItem() #想要移動的物件
        testItem = parent #目標位置的parent
        if testItem == None:
            return False
        while True:
            if testItem is Item: #想移往的位置的parent不能等於自身
                return False
            if testItem == None: #已經搜到最高層，測試通過
                break
            testItem = testItem.parent() #測試更上面一層

        oindex = Item.parent().indexOfChild(Item) #原本的索引位置
        #索引位置變更偵測
        if Item.parent() is parent:
            if index >= oindex and index >= 1:
                index -= 1

        #移動欄位
        Item.parent().takeChild(oindex) #先拔出
        parent.insertChild(index, Item) #再插入
        #確保selection相同
        self.setCurrentItem(Item)
        #發送信號讓main用現存的tree修正資料庫內容
        self.SIGNALdropData.emit(Item.parent())

        return True

class LoBookmarkView(LoTreeView):
    '''bookmark用的View'''
    def dropMimeData(self, parent, index, data, action):
        '''如果drop的parent相同，則成功，否則失敗'''
        if not parent == None: #只能移往根結點
            return False

        Item = self.currentItem() #想要移動的物件
        oindex = self.indexOfTopLevelItem(Item) #原本的索引位置
        #索引位置變更偵測
        if index >= oindex:
            index -= 1

        #移動欄位
        self.takeTopLevelItem(oindex) #先拔出
        self.insertTopLevelItem(index, Item) #再插入
        #確保selection相同
        self.setCurrentItem(Item)
        #發送信號讓main用現存的tree修正資料庫內容
        self.SIGNALdropData.emit(Item)

        return True
